package com.clock.album;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.media.image.PixelMap;

import java.util.ArrayList;

public class ImageAbility extends Ability {
    private ArrayList<Integer> mList=new ArrayList<>();
    private boolean[] mListBool;
    private int index=0;
    private Image checkbox;
    private boolean isShow=true;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_image);
        findComponentById(ResourceTable.Id_back).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });
        PageSlider pageSlider= (PageSlider) findComponentById(ResourceTable.Id_photo_v);
        checkbox= (Image) findComponentById(ResourceTable.Id_check_box_iv);
        mList= getIntent().getIntegerArrayListParam("mList");
        mListBool=getIntent().getBooleanArrayParam("mListBool");
        index=getIntent().getIntParam("index",0);
        if(!mList.isEmpty()){
            PagerProvider pagerProvider=new PagerProvider(this,mList);
            pageSlider.setProvider(pagerProvider);
            pageSlider.setCurrentPage(index);
            setCheckboxIv(mListBool[index]);
        }
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {
                setCheckboxIv(mListBool[i]);
            }
        });
        checkbox.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mListBool[pageSlider.getCurrentPage()]=!mListBool[pageSlider.getCurrentPage()];
                setCheckboxIv(mListBool[pageSlider.getCurrentPage()]);
            }
        });
        findComponentById(ResourceTable.Id_text_sure).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });
        pageSlider.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                isShow=!isShow;
                if (isShow){
                    findComponentById(ResourceTable.Id_top_view).setVisibility(Component.VISIBLE);
                    findComponentById(ResourceTable.Id_bottom_view).setVisibility(Component.VISIBLE);
                }else {
                    findComponentById(ResourceTable.Id_top_view).setVisibility(Component.HIDE);
                    findComponentById(ResourceTable.Id_bottom_view).setVisibility(Component.HIDE);
                }
            }
        });
    }
    @Override
    protected void onBackPressed() {
        Intent intent=new Intent();
        intent.setParam("mListBool",mListBool);
        setResult(1,intent);
        terminateAbility();
    }
    private void setCheckboxIv(boolean bool){
        if(bool){
            checkbox.setPixelMap(ResourceTable.Media_image_selected);
        }else {
            checkbox.setPixelMap(ResourceTable.Media_image_unselected);
        }
    }
}
